package org.papervision3d.core.animation.track
{
	import org.papervision3d.core.animation.keyframe.AbstractKeyFrame;
	import org.papervision3d.objects.DisplayObject3D;

	public class MatrixTrack extends AbstractTrack
	{
		public function MatrixTrack(target:DisplayObject3D)
		{
			super(target);
		}
		
		public override function addKeyFrame(keyframe:AbstractKeyFrame):void
		{		
			if(!keyframe || !keyframe.data || keyframe.data.length < 12)
			{
				return;
			}
			super.addKeyFrame(keyframe);
		}
		
		public override function tick(seconds:Number):void
		{
			super.tick(seconds);
			
			if(current && next)
			{
				matrix.n11 = current.data[0] + (alpha * (next.data[0] - current.data[0]));
				matrix.n12 = current.data[1] + (alpha * (next.data[1] - current.data[1]));
				matrix.n13 = current.data[2] + (alpha * (next.data[2] - current.data[2]));
				matrix.n14 = current.data[3] + (alpha * (next.data[3] - current.data[3]));
				matrix.n21 = current.data[4] + (alpha * (next.data[4] - current.data[4]));
				matrix.n22 = current.data[5] + (alpha * (next.data[5] - current.data[5]));
				matrix.n23 = current.data[6] + (alpha * (next.data[6] - current.data[6]));
				matrix.n24 = current.data[7] + (alpha * (next.data[7] - current.data[7]));
				matrix.n31 = current.data[8] + (alpha * (next.data[8] - current.data[8]));
				matrix.n32 = current.data[9] + (alpha * (next.data[9] - current.data[9]));
				matrix.n33 = current.data[10] + (alpha * (next.data[10] - current.data[10]));
				matrix.n34 = current.data[11] + (alpha * (next.data[11] - current.data[11]));
			}
			else if(current)
			{
				matrix.n11 = current.data[0];
				matrix.n12 = current.data[1];
				matrix.n13 = current.data[2];
				matrix.n14 = current.data[3];
				matrix.n21 = current.data[4];
				matrix.n22 = current.data[5];
				matrix.n23 = current.data[6];
				matrix.n24 = current.data[7];
				matrix.n31 = current.data[8];
				matrix.n32 = current.data[9];
				matrix.n33 = current.data[10];
				matrix.n34 = current.data[11];
			}
		}
			
		public function getValueAtTime(seconds:Number):Array
		{
			super.tick(seconds);
			
			var result :Array = new Array(16);
			
			result[12] = 0;
			result[13] = 0;
			result[14] = 0;
			result[15] = 1;
			
			if(current && next)
			{
				result[0] = current.data[0] + (alpha * (next.data[0] - current.data[0]));
				result[1] = current.data[1] + (alpha * (next.data[1] - current.data[1]));
				result[2] = current.data[2] + (alpha * (next.data[2] - current.data[2]));
				result[3] = current.data[3] + (alpha * (next.data[3] - current.data[3]));
				result[4] = current.data[4] + (alpha * (next.data[4] - current.data[4]));
				result[5] = current.data[5] + (alpha * (next.data[5] - current.data[5]));
				result[6] = current.data[6] + (alpha * (next.data[6] - current.data[6]));
				result[7] = current.data[7] + (alpha * (next.data[7] - current.data[7]));
				result[8] = current.data[8] + (alpha * (next.data[8] - current.data[8]));
				result[9] = current.data[9] + (alpha * (next.data[9] - current.data[9]));
				result[10] = current.data[10] + (alpha * (next.data[10] - current.data[10]));
				result[11] = current.data[11] + (alpha * (next.data[11] - current.data[11]));
			}
			else if(current)
			{
				result[0] = current.data[0];
				result[1] = current.data[1];
				result[2] = current.data[2];
				result[3] = current.data[3];
				result[4] = current.data[4];
				result[5] = current.data[5];
				result[6] = current.data[6];
				result[7] = current.data[7];
				result[8] = current.data[8];
				result[9] = current.data[9];
				result[10] = current.data[10];
				result[11] = current.data[11];
			}
			else
			{
				return null;
			}
			
			return result;
		}
	}
}